const service = require("../../../service/port.js");
Page({
  data: {
    currentTab: 0,
    numa: 1111,
    numb: 2222,
    numc: 3333,
    finshOrderList: [],
    notFinshOrderList: [],
    status: 0,
    pageSize: 4,
    pageNum: 1,
    clsd: 0, //已结算
    noSettlement: 0, //未结算
    earnings: 0, //累计收益
    noData: false,
  },
  getList(callback) {
    let customerId = wx.getStorageSync("userInfo").id;
    service.myIncome(
      {
        tbCustomerreward: {
          customerId: customerId,
          status: this.data.status,
        },
        queryRequest: {
          pageSize: this.data.pageSize,
          pageNum: this.data.pageNum,
        },
      },
      (res) => {
        console.log(res);
        callback(res);
      }
    );
  },

  onLoad: function () {
    const id = wx.getStorageSync("userInfo").id;

    var self = this;
    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        });
      },
    });
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(function (res) {
      console.log(res);
      self.setData({
        finshOrderList: res.data.records,
        // winHeight: res.data.total * 240
      });
      wx.hideLoading();
    });

    service.getCLSDNum(id, (res) => {
      //获取已结算金额
      // console.log(res)
      this.setData({
        clsd: res.data.settled || 0,
      });
    });
    service.getearnings(id, (res) => {
      //获取累计收益金额
      // console.log(res)
      this.setData({
        earnings: res.data.settled || 0,
      });
    });
    service.getNoSettlement(id, (res) => {
      //获取未结算金额
      // console.log(res)
      this.setData({
        noSettlement: res.data.settled || 0,
      });
    });
  },

  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        noData: false,
      });
    }
  },

  bindChange: function (e) {
    console.log(e);
    var self = this;
    if (e.detail.current == 0) {
      this.data.status = "0";
      this.getList(function (res) {
        if (res.data.records.length == 0) {
          self.setData({
            noData: true,
          });
        } else if (res.data.records.length <= 3) {
          self.setData({
            finshOrderList: res.data.records,
            winHeight: 3 * 225,
          });
        } else {
          self.setData({
            finshOrderList: res.data.records,
            winHeight: res.data.records.length * 225,
          });
        }
      });
    } else if (e.detail.current == 1) {
      this.data.status = "1";
      this.getList(function (res) {
        if (res.data.records.length == 0) {
          self.setData({
            noData: true,
          });
        } else if (res.data.records.length <= 3) {
          self.setData({
            notFinshOrderList: res.data.records,
            winHeight: 3 * 225,
          });
        } else {
          self.setData({
            notFinshOrderList: res.data.records,
            winHeight: res.data.records.length * 225,
          });
        }
      });
    }
    self.setData({
      currentTab: e.detail.current,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  // 截获竖向滑动
  catchTouchMove:function(res){
    return false
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    if (this.data.status == "0") {
      this.getList(function (res) {
        self.setData({
          finshOrderList: res.data.records,
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      });
    } else if (this.data.status == "1") {
      this.getList(function (res) {
        self.setData({
          notFinshOrderList: res.data.records,
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noData) return;
    // 停止下拉刷新
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    const finshOrderList = this.data.finshOrderList;
    const notFinshOrderList = this.data.notFinshOrderList;
    let pageNum = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++pageNum,
    });
    const self = this;
    if (this.data.status == "0") {
      self.getList(function (res) {
        console.log(res);
        if (res.data.records.length == 0) {
          self.setData({
            noData: true,
          });
        } else {
          res.data.records.forEach((item) => {
            finshOrderList.push(item);
          });
          let height = finshOrderList.length - 1;
          self.setData({
            finshOrderList: finshOrderList,
            winHeight: height * 220,
          });
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      });
    } else if (this.data.status == "1") {
      self.getList(function (res) {
        if (res.data.records.length == 0) {
          self.setData({
            noData: true,
          });
        } else {
          res.data.records.forEach((item) => {
            notFinshOrderList.push(item);
          });
          let height = notFinshOrderList.length;
          self.setData({
            notFinshOrderList: notFinshOrderList,
            winHeight: height * 225,
          });
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
